package com.zlx.sharelive.activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.AddFriendBean;
import com.zlx.module_base.base_api.res_data.BaseBean;
import com.zlx.module_base.base_api.res_data.MsgBean;
import com.zlx.module_base.base_api.res_data.MsgListBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 消息列表
 */
public class MessageListAc extends BaseAc {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private int page = 1;

    private List<MsgListBean.InfoDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<MsgListBean.InfoDTO.RecordsDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_message_list;
    }

    @Override
    public void initViews() {
        super.initViews();

        initRecyclerView();
        getMsgList(page);
    }

    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecyclerViewAdapter<MsgListBean.InfoDTO.RecordsDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, MsgListBean.InfoDTO.RecordsDTO entity, int position) {
                holder.setText(R.id.tv_title, entity.getMsgTitle());
                holder.setText(R.id.tv_time, entity.getCreateTime());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_message;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (mList.get(position).getType().equals("1")) {
                    //申请伴侣
                    getMsg(mList.get(position).getId());
                } else if (mList.get(position).getType().equals("2")) {
                    //系统消息
                    startActivity(new Intent(MessageListAc.this, MessageDetailsAc.class).putExtra("id",
                            mList.get(position).getId()));
                }
            }
        });
    }

    private void getMsg(String id) {
        ApiUtil.getUserApi().getMsg(SPUtil.getToken(this), id).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<MsgBean>>() {
            @Override
            public void onSuccess(ApiResponse<MsgBean> data) {
                if (null != data.getData()) {
                    if (data.getData().getCode() == 0) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                dialogMessage(data.getData().getInfo().get(0));
                            }
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }

    private void dialogMessage(MsgBean.InfoDTO data) {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(this, R.layout.dialog_add_message, null);
        dialog.setContentView(view);

        ImageView iv_head = view.findViewById(R.id.iv_head);
        GlideImgManager.glideLoader(MessageListAc.this,
                data.getAvatar(), iv_head, 0);

        TextView tv_nickname = view.findViewById(R.id.tv_nickname);
        tv_nickname.setText(data.getNickname());

        ImageView iv_vip = view.findViewById(R.id.iv_vip);
        if (data.getIsvip().equals("2")) {
            iv_vip.setVisibility(View.INVISIBLE);
        } else {
            iv_vip.setVisibility(View.VISIBLE);
        }

        TextView tv_refuse = view.findViewById(R.id.tv_refuse);

        tv_refuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //拒绝
                verifyAddFriend(data.getId(), "2", dialog);
            }
        });

        TextView tv_agree = view.findViewById(R.id.tv_agree);

        tv_agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //同意
                verifyAddFriend(data.getId(), "1", dialog);
            }
        });

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //设置弹出动画
        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

    }

    /**
     * 好友申请验证
     *
     * @param id
     * @param flag
     */
    private void verifyAddFriend(Integer id, String flag, Dialog dialog) {
        AddFriendBean addFriendBean = new AddFriendBean();
        addFriendBean.setId(String.valueOf(id));
        addFriendBean.setFlag(flag);
        ApiUtil.getUserApi().verifyAddFriend(SPUtil.getToken(MessageListAc.this),
                addFriendBean).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<BaseBean>>() {
            @Override
            public void onSuccess(ApiResponse<BaseBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            Toast.makeText(MessageListAc.this, "操作完成", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }

    private void getMsgList(int page) {
        ApiUtil.getUserApi().getMsgList(SPUtil.getToken(this),
                String.valueOf(page)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<MsgListBean>>() {
            @Override
            public void onSuccess(ApiResponse<MsgListBean> data) {
                mList.clear();
                if (data.getData().getCode() == 0) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                if (null != data.getData().getInfo().get(0).getRecords()) {
                                    if (data.getData().getInfo().get(0).getRecords().size() > 0) {
                                        mList.addAll(data.getData().getInfo().get(0).getRecords());
                                        mAdapter.notifyDataSetChanged();
                                    }
                                }
                            }
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }

    @OnClick({R.id.iv_back})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
